package com.yuan.java.thread.linkedQueueTest;

import java.util.concurrent.*;

/**
 * @author yuaneg 2018/12/17
 */
public class LinkedBlockingQueueTest {


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();
        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            try {
                System.out.println("开始生产数据4秒钟一条数据");
                linkedBlockingQueue.put("this is schedule" + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 4, TimeUnit.SECONDS);
        executorService.execute(() -> {
            while (true) {
                try {
                    String message = linkedBlockingQueue.take();
                    System.out.println("消费数据" + message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

    }


}
